<?php

Class _Activity extends CI_Model {

    var $id;
    var $date_act;
    var $price;
    var $description;
    var $placement;

    function __construct() {
        parent::__construct();
    }

    function add_activity($dataactivity) {


// seleccionamos el ultimo id, para asignarte el auto_increment al siguiente
        $query = $this->db->query('SELECT max(id) FROM activity ');
        $row = $query->row_array();
        $id = $row['max(id)'];

        $date = substr($dataactivity['date_act'], 6, 4) . '-' . substr($dataactivity['date_act'], 3, 2) . '-' . substr($dataactivity['date_act'], 0, 2);
        $dataactivity['date_act'] = $date;

        $id++;
        $dataactivity['id'] = $id;
        $this->db->insert('activity', $dataactivity);
    }

    function save_activity($dataactivity) {
        $date = substr($dataactivity['date_act'], 6, 4) . '-' . substr($dataactivity['date_act'], 3, 2) . '-' . substr($dataactivity['date_act'], 0, 2);
        $dataactivity['date_act'] = $date;
// seleccionamos el ultimo id, para asignarte el auto_increment al siguiente
        $this->db->where('id', $dataactivity['id']);
        $this->db->update('activity', $dataactivity);
    }

    function list_activities($min = 0, $qtd = 20) {
        $query = $this->db->query("select *,DATE_FORMAT(date_act, '%d/%m/%Y') as fecha from activity order by date_act desc limit " . $min . "," . $qtd);


        $rows = $query->num_rows();
        $query = $query->result_array();

        return $query;
    }

    function count_activities() {
        $query = $this->db->query("select COUNT(id) as qtd from activity");
        $query = $query->result_array();

        return $query[0]['qtd'];
    }

    function delete($id) {
        $this->db->delete('activity', array('id' => $id));
    }

    function getActivity($id) {

        $query = $this->db->query("select *,DATE_FORMAT(date_act, '%d/%m/%Y') as date_act  from activity where id=" . $id);
        $query = $query->result_array();

        return $query[0];
    }

}

?>
